package com.szzg10.thinking.chapter18;

import java.io.Externalizable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

public class Blips3 implements Externalizable {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		System.out.println("Constructor---");
		Blips3 blips3 = new Blips3();

		//write
		System.out.println("Write---");
		File file = new File(Worm.FILE_NAME_SERIALIZABLE);
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
		objectOutputStream.writeObject(blips3);
		
		objectOutputStream.close();
		System.out.println();
		
		//read
		System.out.println("Read---");
		FileInputStream fileInputStream = new FileInputStream(file);
		ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
		blips3 = (Blips3)objectInputStream.readObject();
		
		objectInputStream.close();
	}
	
	private int i;
	private String str;
	
	public Blips3() {
		System.out.println("Default constructor!");
	}
	
	public Blips3(int i, String str) {
		this.i = i;
		this.str = str;
		
		System.out.println("Another constructor!");
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		i = in.readInt();
		str = (String)in.readObject();
		
		System.out.println("ReadExternal!");
	}

	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		out.writeInt(i);
		out.writeObject(str);
		
		System.out.println("WriteExternal!");
	}
	
	
}
